package yl.hs.bmipfull.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.IDictItemDao;
import yl.hs.bmipfull.daos.system.IDictTypeDao;
import yl.hs.bmipfull.pojo.system.DictItemEntity;
import yl.hs.bmipfull.pojo.system.DictTypeEntity;
import yl.hs.bmipfull.service.system.IDataDictService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * DictTypeServiceImpl 字典类型
 *
 * @author 谢景广
 * @category
 * @date 2021-05-10
 */
@Service
public class DataDictServiceImpl extends SystemBaseService implements IDataDictService {


    @Autowired
    IDictTypeDao typeDao;
    @Autowired
    IDictItemDao itemDao;

    @Override
    public List<DictTypeEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(DictTypeEntity.class);
        return typeDao.queryList(pageQuery.getQuery());
    }

    @Override
    public List<DictTypeEntity> queryDataTypeAll() {
        return typeDao.queryAll();
    }

    @Override
    public DictTypeEntity findDictTypeBySn(String sn) {
        return typeDao.findBySN(sn);
    }

    @Override
    public Boolean checkTypeCodeExists(String typeCode, String sn) {
        if (sn == null) {
            sn = "";
        }
        Integer result = typeDao.countByTypeCode(typeCode, sn);
        return result > 0;
    }

    @Override
    public Boolean checkItemValueExists(String typeSN, String value, String sn) {
        if (sn == null) {
            sn = "";
        }
        var type = findDictTypeBySn(typeSN);
        Integer result = itemDao.countByValue(type.getTypeCode(), value, sn);
        return result > 0;
    }

    @Transactional
    public Integer saveDataType(SubmitModel model) throws Exception {
        var result = 0;
        if (Tl.isEmpty(model.getSn())) {
            var type = new DictTypeEntity();
            type.setSn(queryNewSN(DictTypeEntity.class));
            Tl.getChangeLog(type, model.getData());
            type.setPYFieldValue();
            type.setState((short) 0);
            result = typeDao.insertNew(type);
            writeLog(ActionType.CREATE, type.getSn(), "", type.toJson(), DictTypeEntity.class);
        } else {

            var type = typeDao.findBySN(model.getSn());
            var backJson = type.toJson();
            var changeLog = Tl.getChangeLog(type, model.getData());
            type.setPYFieldValue();
            result = typeDao.update(type);
            //更新数据字典项 类型编码
            List<DictItemEntity> dictItemByCode = findDictItemByTypeSN(type.getSn());
            dictItemByCode.forEach(t -> {
                t.setTypeCode(type.getTypeCode());
                itemDao.update(t);
            });
            writeLog(ActionType.UPDATE, type.getSn(), changeLog, backJson, DictTypeEntity.class);
        }
        return result;
    }

    @Transactional
    public Integer deleteDataItem(String[] snArray) {
        return itemDao.changeStateBySn(snArray, DataState.DELETED.code);
    }

    @Transactional
    public Integer saveDataItem(SubmitModel model) throws Exception {
        var result = 0;
        if (Tl.isEmpty(model.getSn())) {
            var item = new DictItemEntity();
            Tl.getChangeLog(item, model.getData());
            item.setSn(queryNewSN(DictItemEntity.class));
            item.setPYFieldValue();
            result = itemDao.insertNew(item);
            writeLog(ActionType.CREATE, item.getSn(), "", item.toJson(), DictItemEntity.class);
        } else {
            var item = itemDao.findBySN(model.getSn());
            var backJson = item.toJson();
            var changeLog = Tl.getChangeLog(item, model.getData());
            item.setPYFieldValue();
            result = itemDao.update(item);
            writeLog(ActionType.UPDATE, item.getSn(), changeLog, item.toJson(), DictItemEntity.class);
        }
        return result;
    }

    @Transactional
    public Integer deleteDataType(String[] snArray) {

        return typeDao.changeStateBySN(snArray, DataState.DELETED.code);
    }

    @Override
    public DictItemEntity findDataItemBySN(String sn) {

        return itemDao.findBySN(sn);
    }

    @Override
    public List<DictItemEntity> findDictItemByCode(String typeCode) {

        return itemDao.queryAllItemsByTypeCode(typeCode);
    }

    @Override
    public List<DictItemEntity> findDictItemByTypeSN(String typeSN) {
        var type = typeDao.findBySN(typeSN);
        if (type != null) {
            return itemDao.queryAllItemsByTypeCode(type.getTypeCode());
        }
        return null;
    }

    @Override
    public Map<String, Object> queryObjDataAll() {
        var dataTypes = typeDao.queryAll();
        var dataItems = itemDao.queryAllItems();
        var map = new HashMap<String, Object>();
        if (dataTypes != null && dataTypes.size() > 0) {
            dataTypes.forEach(t -> {
                if (!map.containsKey(t.getTypeCode())) {
                    //noinspection Since15
                    map.put(t.getTypeCode(), dataItems.stream().filter(p -> p.getTypeCode().equals(t.getTypeCode())).collect(Collectors.toList()));
                }
            });
        }
        return map;
    }
}
